import { create } from 'zustand';

// Mock 数据
const mockActivities = [
  {
    id: '1',
    name: '2024年春季啤酒品鉴会',
    slug: 'spring-beer-tasting-2024',
    description: '品尝来自世界各地的精酿啤酒，与酿酒师面对面交流',
    images: [
      'https://images.unsplash.com/photo-1558642891-54be180ea339?w=400',
      'https://images.unsplash.com/photo-1535958636474-b021ee887b13?w=400'
    ],
    winnerIds: ['member_003', 'member_005'],
    startTime: '2024-03-15 14:00:00',
    endTime: '2024-03-15 18:00:00',
    status: 'published',
    createdAt: '2024-02-01 10:00:00',
    updatedAt: '2024-02-20 15:30:00'
  },
  {
    id: '2',
    name: 'IPA挑战赛',
    slug: 'ipa-challenge',
    description: '盲品10种不同的IPA，猜中最多者获胜',
    images: [
      'https://images.unsplash.com/photo-1532634922-8fe0b757fb13?w=400'
    ],
    winnerIds: [],
    startTime: '2024-04-01 19:00:00',
    endTime: '2024-04-01 22:00:00',
    status: 'draft',
    createdAt: '2024-03-01 09:00:00',
    updatedAt: '2024-03-10 14:20:00'
  },
  {
    id: '3',
    name: '酿酒工作坊',
    slug: 'brewing-workshop',
    description: '学习基础酿酒技术，亲手酿造属于自己的精酿啤酒',
    images: [
      'https://images.unsplash.com/photo-1612528443702-f6741f70a049?w=400',
      'https://images.unsplash.com/photo-1571613316887-6f8d5cbf7ef7?w=400'
    ],
    winnerIds: ['member_007'],
    startTime: '2024-02-10 10:00:00',
    endTime: '2024-02-10 16:00:00',
    status: 'offline',
    createdAt: '2024-01-15 11:00:00',
    updatedAt: '2024-02-25 09:45:00'
  },
  {
    id: '4',
    name: '比利时啤酒节',
    slug: 'belgian-beer-festival',
    description: '品尝正宗比利时修道院啤酒，了解比利时啤酒文化',
    images: [
      'https://images.unsplash.com/photo-1504502350688-00f5d59bbdeb?w=400'
    ],
    winnerIds: [],
    startTime: '2024-05-20 15:00:00',
    endTime: '2024-05-22 23:00:00',
    status: 'published',
    createdAt: '2024-03-20 13:00:00',
    updatedAt: '2024-03-25 10:15:00'
  },
  {
    id: '5',
    name: '新品发布会 - 夏日果香系列',
    slug: 'summer-fruit-series-launch',
    description: '全新夏日果香系列精酿啤酒发布，首批品鉴机会',
    images: [
      'https://images.unsplash.com/photo-1618885472179-5e474019f2a9?w=400',
      'https://images.unsplash.com/photo-1608270586620-248524c67de9?w=400',
      'https://images.unsplash.com/photo-1559818454-1b46997bfe30?w=400'
    ],
    winnerIds: [],
    startTime: '2024-06-01 18:00:00',
    endTime: '2024-06-01 21:00:00',
    status: 'draft',
    createdAt: '2024-04-01 16:00:00',
    updatedAt: '2024-04-05 11:30:00'
  }
];

const useActivitiesStore = create((set) => ({
  activities: mockActivities,
  loading: false,
  error: null,
  filters: {
    status: 'all' // all, published, draft, offline
  },

  // 获取活动列表
  getActivities: async (filters = {}) => {
    set({ loading: true });
    try {
      // 模拟API调用延迟
      await new Promise(resolve => setTimeout(resolve, 500));
      
      let filteredActivities = [...mockActivities];
      
      // 根据状态筛选
      if (filters.status && filters.status !== 'all') {
        filteredActivities = filteredActivities.filter(
          activity => activity.status === filters.status
        );
      }
      
      set({ 
        activities: filteredActivities, 
        loading: false,
        filters 
      });
    } catch (error) {
      set({ error: error.message, loading: false });
    }
  },

  // 获取单个活动
  getActivityById: (id) => {
    const { activities } = useActivitiesStore.getState();
    return activities.find(activity => activity.id === id);
  },

  // 创建活动
  createActivity: async (activityData) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const newActivity = {
        ...activityData,
        id: Date.now().toString(),
        status: 'draft',
        createdAt: new Date().toISOString(),
        updatedAt: new Date().toISOString()
      };
      
      set((state) => ({
        activities: [...state.activities, newActivity],
        loading: false
      }));
      
      return newActivity;
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  // 更新活动
  updateActivity: async (id, updates) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => ({
        activities: state.activities.map(activity =>
          activity.id === id
            ? { ...activity, ...updates, updatedAt: new Date().toISOString() }
            : activity
        ),
        loading: false
      }));
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  // 删除活动
  deleteActivity: async (id) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => ({
        activities: state.activities.filter(activity => activity.id !== id),
        loading: false
      }));
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  // 上线/下线活动
  toggleActivityStatus: async (id, status) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => ({
        activities: state.activities.map(activity =>
          activity.id === id
            ? { ...activity, status, updatedAt: new Date().toISOString() }
            : activity
        ),
        loading: false
      }));
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  // 设置筛选条件
  setFilters: (filters) => {
    set({ filters });
  },

  // 清除错误
  clearError: () => set({ error: null })
}));

export default useActivitiesStore;